package com.notary.entity.vo;

import com.notary.exception.code.BusinessExceptionCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 统一返回包装类
 * </p>
 *
 * @author ldh
 * @since 2018-11-21
 */
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class JsonResult<T> {
    public static final String SUCCESS_CODE = "200";

    public static final String ERROR_CODE = "0";

    public static final String SUCCESS_DEFAULT_MESSAGE = "操作成功";

    public static final String FAILURE_DEFAULT_MESSAGE = "操作失败";

    private String code;

    private String msg;

    private T data;

    private String remark;

    private Boolean resultSuccess;

    public JsonResult(String code, String msg, T data, String remark) {
        this.resultSuccess = code == SUCCESS_CODE ? true : false;
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.remark = remark;
    }

    public static JsonResult build(String code, String message) {
        return new JsonResult<>(code, message, "", "");
    }

    public static JsonResult build(String code, String message, Object data) {
        return new JsonResult<>(code, message, data, "");
    }

    public static JsonResult success() {
        return new JsonResult<>(SUCCESS_CODE, SUCCESS_DEFAULT_MESSAGE, "", "");
    }

    public static JsonResult success(Object data) {
        return new JsonResult<>(SUCCESS_CODE, SUCCESS_DEFAULT_MESSAGE, data, "");
    }

    public static JsonResult success(String msg, Object data) {
        return new JsonResult<>(SUCCESS_CODE, msg, data, "");
    }

    public static JsonResult success(String msg, Object data, String remark) {
        msg = msg == null ? SUCCESS_DEFAULT_MESSAGE : msg;
        return new JsonResult<>(SUCCESS_CODE, msg, data, remark);
    }

    public static JsonResult fail() {
        return new JsonResult<>(ERROR_CODE, FAILURE_DEFAULT_MESSAGE, "", "");
    }

    public static JsonResult fail(String msg) {
        return new JsonResult<>(ERROR_CODE, msg, "", "");
    }

    public static JsonResult fail(BusinessExceptionCode code) {
        return new JsonResult(code.getCode(), code.getMessage(), "", "");
    }

    public static JsonResult fail(String msg, Object data) {
        return new JsonResult<>(ERROR_CODE, msg, data, "");
    }

    public static JsonResult fail(String code, String msg, Object data) {
        return new JsonResult<>(code, msg, data, "");
    }

    public static JsonResult fail(String code, String msg) {
        return new JsonResult<>(code, msg, "", "");
    }
}
